package extras.salvation.api.methods;

import java.awt.Point;
import java.awt.Rectangle;

import org.rsbuddy.widgets.Bank;

import com.rsbuddy.script.methods.Keyboard;
import com.rsbuddy.script.methods.Mouse;
import com.rsbuddy.script.task.Task;
import com.rsbuddy.script.util.Random;
import com.rsbuddy.script.util.Timer;
import com.rsbuddy.script.wrappers.Component;
import com.rsbuddy.script.wrappers.Item;
import com.rsbuddy.script.wrappers.Widget;

/**
 * @author Salvation
 * @version 1.0
 * @since 20-08-2011
 */
public final class ExtendedBanking {

	/**
	 * @author Salvation
	 * @param ids The ids to withdraw.
	 * @param counts The count for each corresponding id to withdraw.
	 */
	public static void withdraw(final int[] ids, final int[] counts) {
		for (int i = 0; i < ids.length; i++) {
			withdraw(ids[i], counts[i]);
		}
		Task.sleep(1000, 1500);
	}

	/**
	 * @authro Salvation
	 * @param ids The ids to withdraw.
	 * @param count The count for each id to withdraw.
	 */
	public static void withdraw(final int[] ids, final int count) {
		for (int i = 0; i < ids.length; i++) {
			withdraw(ids[i], count);
		}
		Task.sleep(1000, 1500);
	}

	/**
	 * @author Salvation
	 * @param ids The ids to possibly withdraw.
	 * @param count The count for each id to withdraw.
	 */
	public static void withdrawEitherOf(final int[] ids, final int count) {
		for (final int i : ids) {
			if (Bank.getCount(i) > 0) {
				Bank.withdraw(i, count);
				return;
			}
		}
	}

	/**
	 * @author Salvation
	 * @param ids The ids of this potion where ids[0] is the 1-dosed variant.
	 * @return The doses of this potion found in the Bank.
	 */
	public static int getPotionDoseCount(int[] ids) {
		int count = 0;
		for (int i = 0; i < 4; i++) {
			count += Bank.getCount(ids) * (i + 1);
		}
		return count;
	}
	
	/**
	 * @author xAPI
	 * @author Salvation
	 */
	private static boolean withdraw(final int itemId, final int count) {
		if (Bank.isOpen()) {
			if (count >= -1) {
				Widget widget = Bank.getWidget();
				Item item = Bank.getItem(itemId);
				if (item != null) {
					Component itemComponent = item.getComponent();
					if (itemComponent == null) {
						return false;
					}
					if (itemComponent.getRelLocation().equals(new Point(0, 0))) {
						Component bankTab = widget
						.getComponent(Bank.COMPONENT_BANK_TABS[0]);
						if (bankTab.isValid()) {
							bankTab.click();
							Task.sleep(1000, 1300);
						}
					}
					Component container = widget.getComponent(93);
					Rectangle rectangle = container.getViewportRect();
					if (!rectangle.contains(itemComponent.getBoundingRect())) {
						Point p = container.getAbsLocation();
						Mouse.move(Random.nextGaussian(p.x, p.x
								+ rectangle.width, rectangle.width / 2), Random
								.nextGaussian(p.y, p.y + rectangle.height,
										rectangle.height / 2));
						Timer limit = new Timer(5000);
						while (!rectangle.contains(itemComponent
								.getBoundingRect()) && limit.isRunning()) {
							Mouse.scroll(itemComponent.getAbsLocation().y < container
									.getAbsLocation().y);
							Task.sleep(20, 150);
						}
						if (!rectangle
								.contains(itemComponent.getBoundingRect())) {
							return false;
						}
					}
					countSwitch: switch (count) {
					case -1:
						itemComponent.interact("Withdraw-All but one");
						break;
					case 1:
						itemComponent.click(true);
						break;
					case 0:
						itemComponent.interact("Withdraw-All");
						break;
					default:
						String exactAction = "Withdraw-" + count;
						String[] actions = item.getComponent().getActions();
						if (actions != null && actions.length > 0) {
							for (final String action : item.getComponent().getActions()) {
								if (action != null && action.equals(exactAction) && item.interact(action)) {
									break countSwitch;
								}
							}
							if (itemComponent.interact("Withdraw-X")) {
								Task.sleep(1000, 1300);
								Keyboard.sendText(String.valueOf(count), true);
							}
						} else {
							return false;
						}
					}
					return true;
				}
			}
		}
		return false;
	}
}